import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/login.vue'
import Home from '../views/home/home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '/login',
    component: Login,

  },
  {
    path: '/home',
    name: '/home',
    component: Home,
    children: [
      // 聊天
      {
        name: "/home/chat",
        path: "/home/chat",
        component: () => import("../views/chat/chat.vue"),
      },
      // 通讯录
      {
        name: "/home/friends",
        path: "/home/friends",
        component: () => import("../views/friends/friends.vue"),
      },
      // 已发文
      {
        name: "/home/published",
        path: "/home/published",
        component: () => import("../views/published/published.vue"),
      },
      // 发文详情
      {
        name: "/home/postDetails",
        path: "/home/postDetails",
        component: () => import("../views/published/postDetails.vue"),
      },
      // 已收文 received
      {
        name: "/home/received",
        path: "/home/received",
        component: () => import("../views/received/received.vue"),
      },
      
      // 收文详情
      {
        name: "/home/receiveDetails",
        path: "/home/receiveDetails",
        component: () => import("../views/received/receiveDetails.vue"),
      },
      // 云文件 cloud
      {
        name: "/home/cloud",
        path: "/home/cloud",
        component: () => import("../views/cloud/cloud.vue"),
      },
      // 收藏 collection
      {
        name: "/home/collection",
        path: "/home/collection",
        component: () => import("../views/collection/collection.vue"),
      },
      // 设置
      {
        name: "/home/settine",
        path: "/home/settine",
        component: () => import("../views/settine/settine.vue"),
      },
    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫
// router.beforeEach((to, from, next) => {
//   const token = sessionStorage.getItem("token");
//   const expireTime = sessionStorage.getItem("expireTime");

//   // 如果目标路由是登录页，直接放行
//   if (to.name === '/login') {
//     next();
//     return;
//   }

//   // 判断 token 是否存在，且是否过期
//   if (token && (!expireTime || Date.now() <= expireTime)) {
//    return next();  // token 有效，继续访问目标页面
//   } else {
//     // 如果 token 无效或过期，跳转到登录页
//     sessionStorage.removeItem("token");
//     sessionStorage.removeItem("expireTime");
//     next({ name: '/login' });  // 跳转到登录页面
//   }
// });


export default router
